"use strict";

const api = require("../../utils/api");

// pages/page/list.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    info: {},
    tags: ["录入", ""],
    tabs: ["玩具"],
    activeIndex: 0,
    searchkey: '',
    rooms: [null, null], //rooms[0] 即为玩具列表
  },
  refreshFollowRooms: function () {
    app.api.api_user_info().then(res => {
      this.setData({
        info: res
      })
    })
    app.api.api_toy_list({
      type: 0,
      searchkey: this.data.searchkey
    }).then(res => {
      this.data.rooms[0] = res
      this.setData({
        rooms: this.data.rooms
      })
    })
  },
  refreshCreateRooms: function () {

  },
  refresh: function () {
    switch (this.data.activeIndex) {
      case 0:
        this.refreshFollowRooms()
        break
      case 1:
        this.refreshCreateRooms()
        break
    }
  },

  create: function (e) {
    wx.navigateTo({
      url: 'edit',
    })
  },
  bindKeyInput: function (e) {
    this.data[e.currentTarget.dataset.obj] = e.detail.value
    this.refresh();
  },
  create_toy: function (e) {
    wx.navigateTo({
      url: 'edit',
    })
  },
  detail: function (e) { //发现一个问题，调试 network 里面，只显示部分参数“detail?user_id=1”没显示 relaid，搞了半天。
    let url = 'detail?id=' + e.currentTarget.id;
    wx.navigateTo({
      url: url,
    })
  },
  tabClick: function (e) {
    this.setData({
      activeIndex: parseInt(e.currentTarget.id)
    });
    this.refresh()
  },
  scan_get: function (e) {
    var that = this;
    api.api_scan(function (res) {
      console.log(" scan get info ");
      console.log(res);
      if (res.errMsg == 'scanCode:ok') {
        wx.showModal({
          title: '提示',
          content: '确定借吗？',
          success: sm => {
            if (sm.confirm) {
              api.api_toy_scan_get({
                barcode: res.result
              }).then(res => {
                api.api_showMsg(res);
                that.refresh();
              })
            }
          }
        })
      }
    })
  },
  scan_return: function (e) {
    var that = this;
    api.api_scan(function (res) {
      console.log("scan return info ");
      console.log(res);
      if (res.errMsg == 'scanCode:ok') {
        wx.showModal({
          title: '提示',
          content: '确定还吗？',
          success: sm => {
            api.api_toy_scan_return({
              barcode: res.result
            }).then(res => {
              api.api_showMsg(res);
              that.refresh();
            })
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.refresh()

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})